package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.DownloadInfo;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.download.DownloadManager;
import com.rjwh.dingdong.client.util.RxBus;
import com.rjwh.dingdong.client.util.ToastUtil;
import com.wby.base.AdapterBase;

/**
 * 项目名称：whrjwh-parent
 * 类描述：
 * 创建人：wby
 * 创建时间：2016/6/27 11:01
 * 修改人：wby
 * 修改时间：2016/6/27 11:01
 * 修改备注：
 *
 */
public class DowloadUpdateListAdapter extends AdapterBase<LocalGmaeInfo> {

    private Context context;

    public DowloadUpdateListAdapter(Context context) {
        this.context = context;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    public class ViewHolder {
        public TextView tvAppNmae;
//        public TextView tvAppSize;
        public Button button;
        protected View mRootView;
        private LocalGmaeInfo mData;
        private ImageView imgIco;
//        TextView tvDownloadSize;
        public ViewHolder() {
            mRootView = initView();
            mRootView.setTag(this);
        }

        public View getRootView() {
            return mRootView;
        }


        public View initView() {
            View view = LayoutInflater.from(context).inflate(R.layout.dowload_update_list_item, null);
            imgIco = (ImageView) view.findViewById(R.id.download_update_img_pic);
            tvAppNmae = (TextView) view.findViewById(R.id.download_update_appname_tv);
//            tvDownloadSize = com.rjwh.dingdong.client.util.ViewHolder.get(view, R.id.download_manager_downloadSize_tv);
            button = (Button) view.findViewById(R.id.download_update_start_btn);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DownloadInfo dwinfo = new DownloadInfo();
                    if (mData == null) {
                        ToastUtil.showToast(context, "下载错误");
                        return;
                    }
                    if (AsStrUtils.isEmpty(mData.getDownloadUrl())) {
                        ToastUtil.showToast(context, "下载地址错误");
                        return;
                    }
                    if (AsStrUtils.isEmpty(mData.getPname())) {
                        ToastUtil.showToast(context, "下载名称错误");
                        return;
                    }
                    if (AsStrUtils.isEmpty(mData.getPic())) {
                        ToastUtil.showToast(context, "下载封面错误");
                        return;
                    }
                    dwinfo.setGameId(mData.getPid());
                    dwinfo.setGameName(mData.getPname());
                    if (!AsStrUtils.isEmpty(mData.getDownloadUrl())) {
                        dwinfo.setUrl(mData.getDownloadUrl());
                    }
                    dwinfo.setType(mData.getType());
                    dwinfo.setVersion(mData.getVersion());
                    dwinfo.setGamePic(mData.getPic());
                    DownloadManager.getInstance().addTask(dwinfo);

                    mData.setIsUpdate(2);
                    MyApplication.db.update(mData);

                    getList().remove(mData);
                    notifyDataSetChanged();
                    RxBus.get().post(LocalConstant.RX__POST_DOWNLOAD_REFRESH_LIST, dwinfo);
                }
            });
            return view;
        }

        public void setData(LocalGmaeInfo data) {
            mData = data;
            refreshView();

        }

        public LocalGmaeInfo getData() {
            return mData;
        }

        public void refreshView() {
            tvAppNmae.setText(mData.getPname());
//            String totalLength = Formatter.formatFileSize(context,mData.getTotalLength());
//            tvDownloadSize.setText(totalLength);
            Glide.with(context).load(mData.getPic()).placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder).dontAnimate().into(imgIco);
        }


    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        // TODO Auto-generated method stub
        final LocalGmaeInfo appInfo = getList().get(position);

        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setData(appInfo);
        return holder.getRootView();
    }

    @Override
    protected void onReachBottom() {

    }


}
